package com.ocom.webapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MapKey;
import com.ocom.common.dto.CompanyInfoDto;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.CompanyListInfoVo;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.service.AgentPageService;
import com.ocom.webapi.vo.agentpage.AddVo;
import com.ocom.webapi.vo.agentpage.ModifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AgentPageServiceImpl implements AgentPageService {

    @Autowired
    private CompanyClientService companyClientService;
    /**
     * 代理商列表（AgentPage/onShow）
     * */
    @Override
    public Result AgentPageOnShow(Integer agentId, String agentName, Integer page, Integer size) {
        Long comId = null;
        if (agentId!=null){
            comId= Long.valueOf(agentId);
        }
        String comName = null;
        if (!CommonUtil.isNull(agentName)){
            comName = agentName;
        }
        CompanyListInfoVo companyListInfoVo =new CompanyListInfoVo();
        companyListInfoVo.setComId(comId);
        companyListInfoVo.setComName(comName);
        companyListInfoVo.setPage(1);
        companyListInfoVo.setSize(size);
        companyListInfoVo.setOnly(1);
        return  companyClientService.GetCompanyListInfo(companyListInfoVo);
    }

    /**
     * 代理商信息（AgentPage/info）
     * */
    @Override
    public Result AgentPageInfo(Long agentId) {

        Map<String, Object> map = new HashMap<>();
        map.put("comId",agentId);
        return companyClientService.GetCompanyInfo(map);
    }
    /**
     * 代理商修改（AgentPage/modify）
     * */
    @Override
    public Result AgentPageModify(ModifyVo modifyVo) {

        CompanyInfoDto companyInfoDto = new CompanyInfoDto();
        if (StringUtils.isNotNull(modifyVo.getAgentId())) {
            companyInfoDto.setComId(modifyVo.getAgentId());
        }

        if (StringUtils.isNotEmpty(modifyVo.getAgentName())) {
            companyInfoDto.setComName(modifyVo.getAgentName());
        }
        if (StringUtils.isNotEmpty(modifyVo.getProvince())) {
            companyInfoDto.setProvince(modifyVo.getProvince());
        }
        if (StringUtils.isNotEmpty(modifyVo.getCity())) {
            companyInfoDto.setCity(modifyVo.getCity());
        }
        if (StringUtils.isNotEmpty(modifyVo.getDistrict())) {
            companyInfoDto.setDistrict(modifyVo.getDistrict());
        }
        if (StringUtils.isNotEmpty(modifyVo.getContPerson())) {
            companyInfoDto.setContPerson(modifyVo.getContPerson());
        }
        if (StringUtils.isNotEmpty(modifyVo.getContPhone())) {
            companyInfoDto.setContPhone(modifyVo.getContPhone());
        }
        if (StringUtils.isNotEmpty(modifyVo.getContEmail())) {
            companyInfoDto.setContEmail(modifyVo.getContEmail());
        }
        if (StringUtils.isNotEmpty(modifyVo.getContAddress())) {
            companyInfoDto.setContAddress(modifyVo.getContAddress());
        }
        if (StringUtils.isNotEmpty(modifyVo.getRemark())) {
            companyInfoDto.setRemark(modifyVo.getRemark());
        }
        if (StringUtils.isNotNull(modifyVo.getEndTime())) {
                companyInfoDto.setEndTime(modifyVo.getEndTime());
        }
        if (StringUtils.isNotNull(modifyVo.getXfStatus())) {
            companyInfoDto.setXfStatus(modifyVo.getXfStatus());
        }
        if (StringUtils.isNotNull(modifyVo.getMjStatus())) {
            companyInfoDto.setMjStatus(modifyVo.getMjStatus());
        }

        return companyClientService.UpdateCompanyInfo(companyInfoDto);
    }

    /**
     * 代理商新增（AgentPage/add）
     * */
    @Override
    public Result AgentPageAdd(AddVo addVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        //封装新增单位信息
        map.put(mapKey.getKey_comName(),addVo.getAgentName());
        map.put(mapKey.getKey_province(),addVo.getProvince());
        map.put(mapKey.getKey_city(),addVo.getCity());
        map.put("isAgent",1);
        map.put(mapKey.getKey_agentId(),0);
        map.put(mapKey.getKey_district(),addVo.getDistrict());
        map.put(mapKey.getKey_contPerson(),addVo.getContPerson());
        map.put(mapKey.getKey_contPhone(),addVo.getContPhone());
        map.put(mapKey.getKey_contEmail(),addVo.getContEmail());
        map.put(mapKey.getKey_contAddress(),addVo.getContAddress());
        map.put(mapKey.getKey_xfStatus(),addVo.getXfStatus());
        map.put(mapKey.getKey_mjStatus(),addVo.getMjStatus());
        map.put(mapKey.getKey_remark(),addVo.getRemark());
        map.put(mapKey.getKey_remark(),addVo.getEndTime());
        map.put(mapKey.getKey_createUserid(), SecurityUtils.getYoCiUser().getId());
        Result result = companyClientService.AddCompanyInfo(map);
        if (!result.getCode().equals(200)){
            return result;
        }

        //封装初始化用户信息
        Map parseObject = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
        Map<String, Object> map1 = new HashMap<>();
        map1.put(mapKey.getKey_comId(),parseObject.get(mapKey.getKey_comId()));
        map1.put(mapKey.getKey_userName(),addVo.getUserName());
        map1.put(mapKey.getKey_password(),addVo.getPassword());
        map1.put(mapKey.getKey_role(),addVo.getRole());
        map1.put(mapKey.getKey_deptId(),0);
        map1.put(mapKey.getKey_bnsId(),0);
        return companyClientService.AddCompanyAdminInfo(map1);
    }
}
